define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilumarket/order/order/index' + location.search,
                    add_url: 'xilumarket/order/order/add',
                    edit_url: 'xilumarket/order/order/edit',
                    del_url: 'xilumarket/order/order/del',
                    multi_url: 'xilumarket/order/order/multi',
                    table: 'xilumarket_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User_id'),operate:'LIKE'},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"1":__('Pay_type 1'),"2":__('Pay_type 2'),"3":__('Pay_type 3')}, formatter: Table.api.formatter.normal},
                        //{field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        {field: 'express_price', title: __('Express_price'), operate:'BETWEEN'},
                        {field: 'freight_type', title: __('Freight_type'), searchList: {"1":__('Freight_type 1'),"2":__('Freight_type 2'),"3":__('Freight_type 3'),"4":__('Freight_type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'express_name', title: __('Express_name'), operate: 'LIKE'},
                        {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                        {field: 'pay_status', title: __('Pay_status'), searchList: {"0":__('Pay_status 0'),"1":__('Pay_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_status', title: __('Order_status'), searchList: {"1":__('Order_status 1'),"2":__('Order_status 2'),"3":__('Order_status 3')}, formatter: Table.api.formatter.status},
                        {field: 'remarks', title: __('Remarks'), operate: 'LIKE'},
                        {field: 'order_ip', title: __('Order_ip'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [{
                                name: 'delivery',
                                text: __('Delivery'),
                                title: __('Delivery'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-user',
                                url: 'xilumarket/order/order/set',
                                callback: function (data) {
                                    Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                },
                                visible: function (row) {
                                    //返回true时按钮显示,返回false隐藏
                                    //如果该订单已经指定骑手，则不显示骑手按钮
                                    if((row.delivery && row.delivery.id) || !row.delivery){
                                        return  false;
                                    }
                                    if(row.order_status == 2) {
                                        return false;
                                    }
                                    //如果该订单不属于配送订单，则不显示骑手按钮
                                    return row.freight_type == 4 || row.freight_type =='4';
                                }
                        }]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        set: function() {
            console.log(111111111111111111111)
            Controller.api.bindevent();
        },
        edit: function () {
            var order_id = Config.order_id;
            $('.addremark_text').on('click', function () {
                Layer.prompt({
                    formType: 2,
                    value: '',
                    title: '备注',
                    //area: ['80px', '30px'] //自定义文本域宽高
                },function (value,index) {
                    value = value.trim();
                    if(!value) {
                        Toastr.error('输入备注信息');
                        return false;
                    }
                    Fast.api.ajax({
                        url: 'xilumarket/order/note_edit/ids/'+order_id,
                        data: {admin_note:value},
                    },function () {
                        $('#admin_note').html(value)
                        Layer.close(index);
                    })
                })
                return false;
            });
            //修改收货信息
            $("#address-edit").on('click', function() {
                var name = $.trim($('[name=name]').val());
                var mobile = $.trim($('[name=mobile]').val());
                var address_detail = $.trim($('[name=address_detail]').val());
                var province = $('[name=province]').val();
                var city = $('[name=city]').val();
                var region = $('[name=region]').val();
                if(!name || !mobile || !address_detail){
                    Toastr.error('收货人或联系电话或地址未填写');
                    return false;
                }
                Layer.confirm('确认修改', {
                    btn: ['确认','取消'] //按钮
                }, function(index){
                    Fast.api.ajax({url:'xilumarket/order/address_edit',data:{order_id:order_id,name:name,mobile:mobile,address_detail:address_detail,province_id:province,city_id:city,region_id:region}},function(data,ret){
                        Toastr.success(ret.msg);
                        Layer.close(index);
                        return false;
                    },function (data,ret) {
                        Toastr.error(ret.msg);
                        return false;
                    });
                }, function(){

                })
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
